package com.help10000.rms.ui.dialog

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.view.View
import com.amir.common.api.ApiContents
import com.amir.common.utils.Constant
import com.amir.common.utils.DisplayUtil
import com.amir.common.utils.SPUtils
import com.amir.common.weight.MyClickableSpan
import com.help10000.rms.R
import com.help10000.rms.common.App
import com.help10000.rms.ui.activitys.OpenUrlActivity
import com.help10000.rms.ui.activitys.logins.LoginActivity
import com.lxj.xpopup.core.CenterPopupView
import kotlinx.android.synthetic.main.dialog_rule_reminder.view.*

class PrivacyRemainderDialog(context: Context, app: Application) :
    CenterPopupView(context) {

    private var application = app
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_rule_reminder
    }

    override fun getMaxWidth(): Int {
        return DisplayUtil.getDensityWdith(context) * -DisplayUtil.dip2px(context, 40f);
    }


    override fun onCreate() {
        super.onCreate()
        val content: String = context.getString(R.string.title_privacy_tip)
        val spannableString = SpannableString(content)
        var startIndex = spannableString.toString().indexOf("用户协议")
        spannableString.setSpan(object : MyClickableSpan(Color.parseColor("#4D9BD3")) {
            override fun onClick(widget: View) {
                OpenUrlActivity.startActivity(context, ApiContents.UserRuleUrl)
            }
        }, startIndex, startIndex + 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        startIndex = spannableString.toString().indexOf("隐私政策")
        spannableString.setSpan(object : MyClickableSpan(Color.parseColor("#4D9BD3")) {
            override fun onClick(widget: View) {
                OpenUrlActivity.startActivity(context, ApiContents.UserPrivacyUrl)
            }
        }, startIndex, startIndex + 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        tvPrivacy.setText(spannableString)
        confirm_button.setOnClickListener {
            App.init(application)
//            SPUtils.save("read_privacy", true)
            SPUtils.commit(Constant.read_splash,true)
            context.startActivity(Intent(context, LoginActivity::class.java))
            (context as Activity).finish()
            dialog.dismiss()
        }
        deny_button.setOnClickListener {
            (context as Activity).finish()
            System.exit(0)
        }

    }
}